package cn.hyp.bantang.module.finance.service.basegs;

import cn.hyp.bantang.framework.common.pojo.PageResult;
import cn.hyp.bantang.framework.common.util.object.BeanUtils;
import cn.hyp.bantang.module.finance.controller.admin.basegs.vo.BaseGsPageReqVO;
import cn.hyp.bantang.module.finance.controller.admin.basegs.vo.BaseGsSaveReqVO;
import cn.hyp.bantang.module.finance.dal.dataobject.basegs.BaseGsDO;
import cn.hyp.bantang.module.finance.dal.mysql.basegs.BaseGsMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.hyp.bantang.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.hyp.bantang.module.finance.enums.ErrorCodeConstants.BASE_GS_NOT_EXISTS;

/**
 * 公司基本信息 Service 实现类
 *
 * @author 半唐
 */
@Service
@Validated
public class BaseGsServiceImpl implements BaseGsService {

    @Resource
    private BaseGsMapper baseGsMapper;

    @Override
    public String createBaseGs(BaseGsSaveReqVO createReqVO) {
        // 插入
        BaseGsDO baseGs = BeanUtils.toBean(createReqVO, BaseGsDO.class);
        baseGsMapper.insert(baseGs);
        // 返回
        return baseGs.getSecCode();
    }

    @Override
    public void updateBaseGs(BaseGsSaveReqVO updateReqVO) {
        // 校验存在
        validateBaseGsExists(updateReqVO.getSecCode());
        // 更新
        BaseGsDO updateObj = BeanUtils.toBean(updateReqVO, BaseGsDO.class);
        baseGsMapper.updateById(updateObj);
    }

    @Override
    public void deleteBaseGs(String id) {
        // 校验存在
        validateBaseGsExists(id);
        // 删除
        baseGsMapper.deleteById(id);
    }

    private void validateBaseGsExists(String id) {
        if (baseGsMapper.selectById(id) == null) {
            throw exception(BASE_GS_NOT_EXISTS);
        }
    }

    @Override
    public BaseGsDO getBaseGs(String id) {
        return baseGsMapper.selectById(id);
    }

    @Override
    public PageResult<BaseGsDO> getBaseGsPage(BaseGsPageReqVO pageReqVO) {
        return baseGsMapper.selectPage(pageReqVO);
    }

}